//
//  GYNewsCell.m
//  research
//
//  Created by 章鱼 on 17/3/27.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYNewsCell.h"

@interface GYNewsCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *infoLable;
@property (nonatomic, strong) UILabel *timeLable;
@property (nonatomic, strong) UIImageView *readView;
@property (nonatomic, strong) UILabel * readNumber;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *emptyView;


@end

@implementation GYNewsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initSubUI];
        
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    }

- (void)initSubUI {
    
    _emptyView = [[UIView alloc] init];
    [self.contentView addSubview:_emptyView];
    _emptyView.backgroundColor = [UIColor orz_globalBackground];
    
    _pictureView = [[UIImageView alloc] init];
    _pictureView.backgroundColor = [UIColor orz_assistGreen];
    [self.contentView addSubview:_pictureView];
    _pictureView.layer.masksToBounds = YES;
    _pictureView.layer.cornerRadius = 3;
    
    _titleLable = [[UILabel alloc] init];
    _titleLable.font = [UIFont systemFontOfSize:16];
    _titleLable.font = [UIFont systemFontOfSize:16];
    [self.contentView  addSubview:_titleLable];
    
    _infoLable = [[UILabel alloc] init];
    _infoLable.font = [UIFont systemFontOfSize:12];
    _infoLable.textColor = [UIColor colorWithHex:@"646464" alpha:1];
    [self.contentView  addSubview:_infoLable];
    
    
    _timeLable = [[UILabel alloc] init];
    _timeLable.font = [UIFont systemFontOfSize:11];
    _timeLable.textColor = [UIColor colorWithHex:@"909090" alpha:1];
    [self.contentView  addSubview:_timeLable];
    
    _readView = [[UIImageView alloc] init];
    [self.contentView  addSubview:_readView];
    [_readView setImage:[UIImage imageNamed:@"home_icon_browse"]];
    
    _readNumber = [[UILabel alloc] init];
    _readNumber.font = [UIFont systemFontOfSize:11];
    _readNumber.text = @"300";
    _readNumber.textColor = [UIColor orz_textAssist];
    [self.contentView  addSubview:_readNumber];
    
    _lineView = [[UIView alloc] init];
    [self.contentView addSubview:_lineView];
    _lineView.backgroundColor = [UIColor orz_line];
    
    [_emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(0.f);
        make.height.mas_equalTo(10.f);
    }];
    
    [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_emptyView.mas_bottom );
        make.left.mas_equalTo(12.f);
        make.size.mas_equalTo(CGSizeMake(70, 53));
        make.bottom.mas_offset(-10.f);
    }];
    
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_pictureView.mas_right).offset(10.f);
        make.top.mas_equalTo(self.contentView).offset(10.f);
        make.right.mas_equalTo(-100.f);
        
    }];
    
    [_infoLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_pictureView.mas_right).offset(10.f);
        make.top.mas_equalTo(_titleLable.mas_bottom).offset(5.f);
        make.right.mas_equalTo(-50.f);

    }];
    
    [_timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_pictureView.mas_right).offset(10.f);
        make.top.mas_equalTo(_infoLable.mas_bottom).offset(10.f);
        make.bottom.mas_equalTo(-10.f);
    }];
    
    [_readNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12.f);
        make.bottom.mas_equalTo(-10.f);

    }];
    
    [_readView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_readNumber.mas_left).offset(-2.f);
        make.centerY.mas_equalTo(_readNumber.mas_centerY);
        
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.bottom.mas_equalTo(0.f);
        make.height.mas_equalTo(0.5f);
    }];
    
}

- (void)updateCellWithData:(GYNewsInfoModel *)data {
    _titleLable.text = data.title;
    _infoLable.text = data.summary;
    [_pictureView sd_setImageWithURL:[NSURL URLWithString:data.picture] placeholderImage:nil];
    
    NSString * timeStampString = data.createTime;
    NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"yyyy-MM-dd"];
    _timeLable.text = [NSString stringWithFormat:@"发表于：%@",[objDateformat stringFromDate: date]];
    _readNumber.text = [NSString stringWithFormat:@"%ld",(long)data.readCount];
}

@end
